/**
 * Created by wry on 2015/5/14.
 */

var voucherService = require('../../services/backoffice/backOffice-voucherService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');

exports.addVoucherType = function (req, res) {

    voucherService.addVoucherTypeService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when addVoucherType ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.getVoucherType = function (req, res) {
    voucherService.getVoucherTypeService(function (err, result) {
        if (err) {
            logger.error('Exception happened when getVoucherTypeService ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.addRetailer = function (req, res) {
    var input = req.body;

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
        } else {
            var data = new Array();

            images.forEach(function (image) {
                if (image != null) {
                    data.push(image);
                }
            });

            if (data.length > 0)
                input.retailer_icon = data[0];

            voucherService.addRetailerService(input, function (err, result) {
                if (err) {
                    logger.error('Exception happened when addRetailer ...!' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, err.message));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, result));
                }
            })
        }
    });
};

exports.addMainRetailer = function (req, res) {
    var input = req.body;

    voucherService.addMainRetailerService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when addMainRetailer ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getRetailerList = function (req, res) {
    var parentId = req.params.parentId;

    voucherService.getRetailerListService(parentId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getRetailerList ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.searchRetailerByName = function (req, res) {
    var parentId = req.body.parentId;
    var name = req.body.name;
    var address = req.body.address;
    var communityId = req.body.communityId;

    voucherService.searchRetailerByNameService(parentId, name, address, communityId, function (err, result) {
        if (err) {
            logger.error('Exception happened when searchRetailerByName ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.bindRetailerCommunity = function (req, res) {
    var retailerId = req.body.retailerId;
    var communities = req.body.communities;

    voucherService.bindRetailerCommunityService(retailerId, communities, function (err, result) {
        if (err) {
            logger.error('Exception happened when bindRetailerCommunity ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    });

};
exports.getAllRetailers = function (req, res) {

    voucherService.getAllRetailers(function (err, result) {
        if (err) {
            logger.error('Exception happened when getAllRetailers ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};

exports.getRetailerAgreement = function (req, res) {
    var retailerId = req.params.retailerId;

    voucherService.getRetailerAgreementService(retailerId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getRetailerAgreement ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.addRetailerAgreement = function (req, res) {
    var input = req.body;
    voucherService.addRetailerAgreementService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when addRetailerAgreement ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.deleteAgreement = function (req, res) {
    var id = req.params.id;
    voucherService.deleteAgreementService(id, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteAgreement ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.getAgreementVoucher = function (req, res) {
    voucherService.getAgreementVoucherService(function (err, result) {
        if (err) {
            logger.error('Exception happened when getAgreementVoucher ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getBranchRetailer = function (req, res) {
    var communityId = req.params.communityId;
    var page = req.params.page;

    voucherService.getBranchRetailerService(communityId, page, function (err, result) {
        if (err) {
            logger.error('Exception happened when getBranchRetailer ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                totalPage: Math.ceil(result[0][0].count / listPageSize),
                list: result[1]
            }
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.singleBindRetailer = function (req, res) {
    var input = req.body;
    voucherService.singleBindRetailerService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when singleBindRetailer ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
}

exports.getRetailerExchangeList = function (req, res) {
    var input = req.body;
    voucherService.getRetailerExchangeList(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getRetailerExchangeList ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var result = {data: result[0], totalPage: result[1]};
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};


exports.searchVoucher = function (req, res) {
    var input = req.body;
    var page = req.params.page;
    voucherService.searchVoucherService(input,page, function (err, result) {
        if (err) {
            logger.error('Exception happened when searchVoucher ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                list: result[0],
                totalPage: result[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.getRetailerSettlement = function (req, res) {
    var input = req.body;

    voucherService.getRetailerSettlementService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getRetailerSettlement ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {

            var data = {
                list: result,
                opt: input
            };

            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.retailerBalance = function (req, res) {
    var input = req.body;

    voucherService.retailerBalanceService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getRetailerSettlement ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {

            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.getVoucherStatistics = function (req, res) {

    var input = req.body;
    voucherService.getVoucherStatistics(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getVoucherStatistics ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var result = {list: result[0], totalPage: result[1]};
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};

exports.getConsumeStatistics = function (req, res) {

    var input = req.body;
    voucherService.getConsumeStatistics(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getConsumeStatistics ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var result = {list: result[0], totalPage: result[1]};
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};

exports.getIsSettlementProfile = function (req, res) {

    var input = req.body;
    voucherService.getIsSettlementProfile(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getIsSettlementProfile ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var result = {list: result[0], totalPage: result[1]};
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};